﻿<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <title>编辑消息模板</title>
    <link rel="stylesheet" href="~/lib/element-ui/theme-chalk/index.css">
    <link rel="stylesheet" href="~/css/default/style.css">
</head>

<body class="mainbody">
    <div id="app" v-cloak>
        <div class="location">
            <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item>
                    <i class="el-icon-s-home"></i>
                    管理中心
                </el-breadcrumb-item>
                <el-breadcrumb-item>首页</el-breadcrumb-item>
                <el-breadcrumb-item>会员设置</el-breadcrumb-item>
                <el-breadcrumb-item>编辑模板</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="content-box">
            <el-tabs v-model="activeName" type="card">
                <el-tab-pane label="基本信息" name="info">
                    <div class="tab-content">
                        <el-form ref="editForm" :model="model" :rules="rules" :label-position="labelPosition" label-width="120px">
                            <el-form-item prop="type" label="模板类型">
                                <el-radio-group v-model="model.type">
                                    <el-radio-button label="1">邮件</el-radio-button>
                                    <el-radio-button label="2">短信</el-radio-button>
                                    <el-radio-button label="3">微信</el-radio-button>
                                </el-radio-group>
                            </el-form-item>
                            <el-form-item prop="callIndex" label="调用名称">
                                <el-input v-model="model.callIndex" placeholder="必填，只允许英文数字下划线"></el-input>
                            </el-form-item>
                            <el-form-item prop="title" label="模板标题">
                                <el-input v-model="model.title" placeholder="必填，128字符内"></el-input>
                            </el-form-item>
                            <el-form-item prop="content" label="模板内容">
                                <el-input type="textarea" :rows="5" v-model="model.content" placeholder="必填，支持HTML"></el-input>
                            </el-form-item>
                            <el-form-item label="更新时间" v-if="model.id>0">
                                {{model.updateBy}} {{model.updateTime}}
                            </el-form-item>
                        </el-form>
                    </div>
                </el-tab-pane>
            </el-tabs>
        </div>

        <div class="footer-box">
            <div class="footer-btn">
                <el-button type="primary" @@click="submitForm">确认保存</el-button>
                <el-button plain @@click="resetForm">取消返回</el-button>
            </div>
        </div>
    </div>

    <script src="~/lib/vue/vue.js"></script>
    <script src="~/lib/element-ui/index.js"></script>
    <script src="~/lib/axios/axios.min.js"></script>
    <script src="~/lib/babel-polyfill/polyfill.min.js"></script>
    <script src="~/js/axios.config.js"></script>
    <script>
        //初始化VUE
        var ve = new Vue({
            el: '#app',
            data: {
                activeName: 'info',//当前选项卡
                labelPosition: 'right',//表单对齐方式
                model: {
                    id: 0,
                    type: null,
                    callIndex: null,
                    title: null,
                    content: null,
                    isSystem: 0,
                    updateTime: null
                },
                rules: {
                    type: [
                        { required: true, message: '请选择模板类型', trigger: 'change' }
                    ],
                    callIndex: [
                        { required: true, message: '请输入模板调用名称', trigger: 'blur' },
                        { pattern: /^[a-zA-Z0-9_]*$/, message: '只能是字母数字下划线', trigger: 'blur' }
                    ],
                    title: [
                        { required: true, message: '请输入模板标题', trigger: 'blur' }
                    ],
                    content: [
                        { required: true, message: '请输入模板内容', trigger: 'blur' }
                    ]
                }
            },
            methods: {
                //初始化数据
                initData: function () {
                    let _this = this; //当前页面
                    let id = GetQueryValue("id"); //获取ID
                    if (id) {
                        AxiosAjax({
                            url: '/notify/template/' + id,
                            loading: true,
                            success: function (res) {
                                //赋值给model
                                Object.keys(_this.model).forEach(key => { _this.model[key] = res.data[key] });
                                //console.log(_this.model);
                            }
                        });
                    }
                },
                submitForm: function () {
                    let _this = this; //当前页面
                    //多个表单验证
                    const f1 = new Promise((resolve, reject) => {
                        _this.$refs['editForm'].validate((valid) => {
                            if (valid) resolve()
                        });
                    });
                    Promise.all([f1]).then(() => {
                        //提交表单,Id大于0则修改，否则添加
                        if (_this.model.id > 0) {
                            AxiosAjax({
                                method: 'put',
                                url: '/notify/template/' + _this.model.id,
                                data: _this.model,
                                loading: true,
                                successMsg: '消息模板修改成功',
                                success: function (res) {
                                    //可跳转加列表页
                                    _this.linkUrl('/member/templatelist');
                                }
                            });
                        } else {
                            AxiosAjax({
                                method: 'post',
                                url: '/notify/template',
                                data: _this.model,
                                loading: true,
                                successMsg: '消息模板添加成功',
                                success: function (res) {
                                    //重置表单
                                    _this.$refs['editForm'].resetFields();
                                }
                            });
                        }
                    })
                },
                //跳转链接
                linkUrl: function (url) {
                    window.location.href = url;
                },
                resetForm() {
                    window.history.back(-1);
                }
            },
            created: function () {
                //初始化数据
                this.initData();
            }
        })
    </script>
</body>
</html>
